/**
 * 
 */
package weibo4j.crawler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import weibo4j.database.DataBaseUtil;


/**
 * @author longrui
 *
 */

public class QueueBuffer {
	public static final int USER = 0;
	public static final int STATUS = 1;
	public static final int COMMENT = 2;
	
	public int type;
	private Connection conn;
	private long length;
	private PreparedStatement pstmt;
	private ResultSet rs;
	private String sql;
	
	public QueueBuffer(int type, Connection conn){
		this.type = type;
		this.conn = conn;
		pstmt = null;
		rs = null;
		sql = null;
	}

	public long getLength() throws SQLException {
		switch(type){
		case QueueBuffer.USER:
			sql = "select count(*) as cnt from queue4user";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			rs.next();
			length = rs.getLong(1);
			break;
		case QueueBuffer.STATUS:
			sql = "select count(*) as cnt from queue4status";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			rs.next();
			length = rs.getLong(1);
			break;
		case QueueBuffer.COMMENT:
			sql = "select count(*) as cnt from queue4comment";
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			rs.next();
			length = rs.getLong(1);
			break;
		default:
			length = 0;
			break;
		}
		rs.close();
		pstmt.close();
		return length;
	}
	
	public QueueRecord peek()throws SQLException{
		QueueRecord rec = null;
		switch(type){
			case QueueBuffer.USER:
				sql = "select * from queue4user order by enqueue_time limit 1";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next()){
					rec = new UserInfoQueueRecord(rs.getLong(1), rs.getString(2));
				}
				break;
			case QueueBuffer.STATUS:
				sql = "select * from queue4status order by enqueue_time limit 1";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next()){
					rec = new StatusQueueRecord(rs.getLong(1), rs.getString(2));
				}
				break;
			case QueueBuffer.COMMENT:
				sql = "select * from queue4comment order by enqueue_time limit 1";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if(rs.next()){
					rec = new CommentQueueRecord(rs.getLong(1), rs.getString(2));
				}
				break;
			default:
				break;
		}
		rs.close();
		pstmt.close();
		return rec;
	}
	
	public boolean contains(QueueRecord rec) throws SQLException{
		int count = 0;
		switch(type){
			case QueueBuffer.USER:
				sql = "select count(*) as cnt from queue4user where user_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, ((UserInfoQueueRecord)rec).userId);
				rs = pstmt.executeQuery();
				rs.next();
				count = rs.getInt(1);
				break;
			case QueueBuffer.STATUS:
				sql = "select count(*) as cnt from queue4status where status_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, ((StatusQueueRecord)rec).statusId);
				rs = pstmt.executeQuery();
				rs.next();
				count = rs.getInt(1);
				break;
			case QueueBuffer.COMMENT:
				sql = "select count(*) as cnt from queue4comment where commented_status_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, ((CommentQueueRecord)rec).commentedStatusId);
				rs = pstmt.executeQuery();
				rs.next();
				count = rs.getInt(1);
				break;
			default:
				break;
		}
		rs.close();
		pstmt.close();
		return count > 0;
	}
	
	public void clear() throws SQLException{
		switch(type){
			case QueueBuffer.USER:
				sql = "delete from queue4user";
				pstmt = conn.prepareStatement(sql);
				pstmt.executeQuery();
				break;
			case QueueBuffer.STATUS:
				sql = "delete from queue4status";
				pstmt = conn.prepareStatement(sql);
				pstmt.executeQuery();
				break;
			case QueueBuffer.COMMENT:
				sql = "delete from queue4comment";
				pstmt = conn.prepareStatement(sql);
				pstmt.executeQuery();
				break;
			default:
				break;
		}
		pstmt.close();
		length = 0;
	}
	
	/**
	 * Remove the specified entry by rec
	 * @param rec
	 * @throws SQLException
	 */
	public void remove(QueueRecord rec) throws SQLException{
		//System.out.println(rec == null);
		int ret = 0;
		switch(type){
			case QueueBuffer.USER:
				sql = "delete from queue4user where user_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, ((UserInfoQueueRecord)rec).userId);
				ret = pstmt.executeUpdate();
				break;
			case QueueBuffer.STATUS:
				sql = "delete from queue4status where status_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, ((StatusQueueRecord)rec).statusId);
				ret = pstmt.executeUpdate();
				break;
			case QueueBuffer.COMMENT:
				sql = "delete from queue4comment where commented_status_id = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setLong(1, ((CommentQueueRecord)rec).commentedStatusId);
				ret = pstmt.executeUpdate();
				break;
			default:
				break;
		}
		if(ret > 0)
			length--;
	}
	
	public QueueRecord poll() throws SQLException{
		QueueRecord resultValue = peek();
		System.out.println("is null? " + (resultValue == null));
		remove(resultValue);
		return resultValue;
	}
	
	public boolean add(QueueRecord rec) throws Exception{
	
		boolean ret = false;
		switch(type){
			case QueueBuffer.USER:
				//ret = DataBaseUtil.insert(conn, "queue4user", UserInfoQueueRecord.getFieldString(),
				//		UserInfoQueueRecord.getEmptyValueString(), ((UserInfoQueueRecord)rec).getRecord());
				ret = DataBaseUtil.executeStatement(conn, ((UserInfoQueueRecord)rec).getUserInfoQueueRecordSQLString());
				break;
			case QueueBuffer.STATUS:
				//ret = DataBaseUtil.insert(conn, "queue4status", StatusQueueRecord.getFieldString(),
				//		StatusQueueRecord.getEmptyValueString(), ((StatusQueueRecord)rec).getRecord());
				ret = DataBaseUtil.executeStatement(conn, ((StatusQueueRecord)rec).getStatusQueueRecordSQLString());
				break;
			case QueueBuffer.COMMENT:
				//ret = DataBaseUtil.insert(conn, "queue4comment", CommentQueueRecord.getFieldString(),
				//		CommentQueueRecord.getEmptyValueString(), ((CommentQueueRecord)rec).getRecord());
				ret = DataBaseUtil.executeStatement(conn, ((CommentQueueRecord)rec).getCommentQueueRecordSQLString());
				break;
			default:
				break;
		}
		if(ret){
			length++;
		}
		return ret;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	}

	

}
